var express = require('express');
var router = express.Router();
const { DetailModel, DoctorModel } = require('../model/model');

/* GET users listing. */
router.get('/', function (req, res, next) {
    res.send('respond with a resource');
});


router.get('/show', async (req, res) => {
    let data = await DetailModel.find()
    res.send({
        code: 200,
        msg: '查询成功',
        data
    })
})

router.post('/add', async (req, res) => {
    let body = req.body
    await DetailModel.create(body)
    res.send({
        code: 200,
        msg: '添加成功'
    })
})

//修改

router.post('/edit', async (req, res) => {
    const { _id, ...hospitalData } = req.body;  // 获取 _id 和其他字段
    try {
        const result = await  DetailModel.updateOne({ _id }, hospitalData);
        if (result.modifiedCount > 0) {
            res.send({ code: 203, msg: '修改成功' });
        } else {
            res.send({ code: 400, msg: '未作任何修改' });
        }
    } catch (error) {
        console.error('数据库更新失败:', error);
        res.send({ code: 500, msg: '服务器错误' });
    }
});

router.get('/ShowDoctor', async (req, res) => {
    let data = await DoctorModel.find()
    res.send({
        code: 200,
        msg: '查询成功',
        data
    })
})


module.exports = router;